<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use app\common\model\CpChannel;
use app\services\Settlement;
/**
 * 游戏管理
 * Class AdminUser
 * @package app\admin\controller
 */
class CpGame extends AdminBase
{

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * CP index
     * @param string $keyword
     * @param int $page
     * @return mixed
     */
    public function index()
    {
        $map = [];
        $page = $data['page'] ?? 1;
        if (!empty(input('name'))) {
            $map[] = ['name', 'like', "%".input('name')."%"];
        }

        $game_list = CpChannel::get_list($map, 'id desc', $page, input());

        return $this->fetch('index', ['list' => $game_list]);
    }

    /**
     * 添加CP
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存CP
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate_result = $this->validate($data, 'CpGame');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {

                if (CpChannel::insert($data)) {
                    $this->success('保存成功', '/admin/cpgame');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑CP
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $game = CpChannel::find($id);
        return $this->fetch('edit', ['game' => $game]);
    }

    /**
     * 更新cp
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {

            $data = $this->request->post();
            $id = $data['id'];
            if ( CpChannel::update($data, ['id' => $id])) {
                $this->success('更新成功', '/admin/cpgame');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 删除cp
     */
    public function delete($id)
    {
        if (CpChannel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


}
